package com.zjut.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjut.dao.QuestionsDao;
import com.zjut.domain.FinalQuestions;
import com.zjut.domain.Paper;
import com.zjut.domain.Questions;
import com.zjut.service.impl.QuestionsServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionsService implements QuestionsServiceImpl {
    @Autowired
    private QuestionsDao questionsDao;

    @Override
    public boolean save(Questions questions) {
        return questionsDao.insert(questions) > 0;
    }

    @Override
    public boolean update(Questions questions) {
        return questionsDao.updateById(questions) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return questionsDao.deleteById(id) > 0;
    }

    @Override
    public Questions getById(Integer id) {
        return questionsDao.selectById(id);
    }

    @Override
    public List<Questions> getAll() {
        return questionsDao.selectList(null);
    }

    @Override
    public IPage<Questions> getPage(Integer currentPage, Integer pageSize, Questions questions) {
        LambdaQueryWrapper<Questions> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(questions.getQuestion()), Questions::getQuestion, questions.getQuestion());
        lqw.eq(Strings.isNotEmpty(questions.getType()), Questions::getType, questions.getType());
        lqw.eq(questions.getChapter() != null, Questions::getChapter, questions.getChapter());
        lqw.eq(questions.getSection() != null, Questions::getSection, questions.getSection());
        lqw.eq(Questions::getStatus,true);
        lqw.orderByAsc(Questions::getChapter, Questions::getSection);

        IPage page = new Page(currentPage, pageSize);
        questionsDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public IPage<Questions> getPage(Integer currentPage, Integer pageSize, Questions questions, Double difficultyMin, Double difficultyMax) {
        LambdaQueryWrapper<Questions> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(questions.getQuestion()), Questions::getQuestion, questions.getQuestion());
        lqw.eq(Strings.isNotEmpty(questions.getType()), Questions::getType, questions.getType());
        lqw.eq(questions.getChapter() != null, Questions::getChapter, questions.getChapter());
        lqw.eq(questions.getSection() != null, Questions::getSection, questions.getSection());
        lqw.between(Questions::getDifficulty, difficultyMin, difficultyMax);
        lqw.eq(Questions::getStatus,true);
        lqw.orderByAsc(Questions::getChapter, Questions::getSection);
        IPage page = new Page(currentPage, pageSize);
        questionsDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public List<FinalQuestions> selectQuestions(String type, Paper paper) {
        if(paper.getMaxChapter()!=paper.getMinChapter()){
            return questionsDao.selectQuestions1(type, paper);
        }
        return questionsDao.selectQuestions2(type, paper);

    }
    @Override
    public List<Questions> selectByIds(List<Integer> idList){
        LambdaQueryWrapper<Questions> lqw = new LambdaQueryWrapper<>();
        lqw.in(Questions::getId, idList);
        return questionsDao.selectList(lqw);
    }
}
/*

 */